import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:search_people/bean/search/search_list.dart';
import 'package:search_people/utils/img_util.dart';
import 'package:search_people/utils/util.dart';

import '../../entrance/route/routes.dart';
import '../../value/topcolors.dart';
import 'logic.dart';

class SearchResultCell extends StatelessWidget {
  const SearchResultCell({
    Key? key,
    this.keywords,
    required this.md,
    this.words,
  }) : super(key: key);
  final String? keywords;
  final Records md;
  final List<String>? words;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(RouterPath.person_detail, arguments: md.id);
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.all(15.r),
        margin: EdgeInsets.symmetric(vertical: 8.r),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: 1.sw / 4,
              height: 1.sw / 4 * 7 / 5,
              child: loadImage(
                url: md.image,
                width: 1.sw / 4,
                height: 1.sw / 4 / 0.8,
              ),
            ),
            SizedBox(
              width: 15.r,
            ),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                RichText(
                  text: TextSpan(
                      children: getTextSpanList(md.name ?? '',
                          fontSize: 18.r, txtColor: TopColors.black_33)),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
                SizedBox(
                  height: 3.r,
                ),
                RichText(
                  text: TextSpan(
                      children: getTextSpanList(
                          md.birthday?.substring(0, 10) ?? '',
                          fontSize: 14.sp)),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
                SizedBox(
                  height: 3.r,
                ),
                Container(
                  height: 20.r,
                  alignment: Alignment.centerLeft,
                  child: CustomScrollView(
                    scrollDirection: Axis.horizontal,
                    slivers: [
                      SliverToBoxAdapter(
                        child: RichText(
                          text: TextSpan(
                              children: getTextSpanList(
                                  md.currentPosition ?? '',
                                  fontSize: 14.r)),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(
                  height: 7.r,
                ),
                RichText(
                  text: TextSpan(
                      children:
                          getTextSpanList(md.inCharge ?? '', fontSize: 13.r)),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 3,
                ),
              ],
            ))
          ],
        ),
      ),
    );
  }

  TextSpan _highlightText({
    required String t,
    double fontSize = 17,
  }) {
    return _getText(
      t: t,
      color: TopColors.main_color,
      fontSize: fontSize,
    );
  }

  TextSpan _getText({
    required String t,
    Color? color,
    double fontSize = 17,
  }) {
    return TextSpan(
      text: t,
      style: TextStyle(
        fontSize: fontSize,
        color: color,
      ),
    );
  }

  ///textContent      需要显示的文字内容
  ///frontContent     需要另外添加在最前面的文字
  ///fontSize         需要显示的字体大小
  ///fontColor        需要显示的正常字体颜色
  ///selectFontColor  需要显示的搜索字体颜色
  List<TextSpan> getTextSpanList(
    String textContent, {
    String frontContent = '',
    Color? txtColor,
    var fontSize = 17,
  }) {
    List<String> _words = words ?? [];
    List<TextSpan> textSpanList = [];

    if (frontContent.isEmpty == false) {
      textSpanList.add(TextSpan(
          text: frontContent,
          style: TextStyle(
              fontSize: fontSize, color: txtColor ?? TopColors.black_66)));
    }

    if (isZeroList(_words) && !isNilStr(keywords)) {
      _words = [keywords!];
    }

    if (isZeroList(_words)) {
      textSpanList.add(TextSpan(
        text: textContent,
        style: TextStyle(
          fontSize: fontSize,
          color: txtColor ?? TopColors.black_66,
        ),
      ));
      return textSpanList;
    }

    var list = split(textContent);
    for (var e in list) {
      textSpanList.add(e.isTag
          ? _highlightText(t: e.t, fontSize: fontSize)
          : _getText(
              t: e.t,
              color: txtColor ?? TopColors.black_66,
              fontSize: fontSize));
    }

    return textSpanList;
  }

  List<TagText> split(String t) {
    List<TagText> list = [];

    for (var i = 0; i < words!.length; i++) {
      var c = words![i];

      if (i == 0) {
        list.addAll(stringSplit(t, c));
      } else {
        if (list.isEmpty) {
          continue;
        }
        var count = list.length;
        for (var j = 0; j < count; j++) {
          var e = list[j];
          if (e.isTag) {
            continue;
          }
          if (e.t.contains(c)) {
            list.removeAt(j);
            list.insertAll(j, stringSplit(e.t, c));
          }
        }
      }
    }

    return list;
  }

  List<TagText> stringSplit(String t, String c) {
    var l = t.split(c).map((e) => TagText(t: e)).toList();
    var idxs = List.generate(l.length - 1, (index) => index + 1);
    var bageIdxs = idxs.map((e) => e * 2 - 1);
    for (var i in bageIdxs) {
      l.insert(i, TagText(t: c, isTag: true));
    }
    return l;
  }
}

class TagText {
  String t;
  bool isTag;
  TagText({
    required this.t,
    this.isTag = false,
  });
}
